const app = getApp();
const pageSize = 15;

const cardTypes = [{
  value: "",
  name: "全部"
}, {
  value: "2",
  name: "普通卡"
}, {
  value: "1",
  name: "特殊卡"
}];
const cardStatuses = [{
  value: "",
  name: "全部"
}, {
  value: "1",
  name: "正常"
}, {
  value: "2",
  name: "挂失"
}]
Page({

  data: {
    conditions: { //存放搜索条件
      user_keyword: "",
      start_money: "",
      end_money: "",
      cardTypeIndex: "",
      cardStatusIndex: "",
    },
    cardTypes, //水卡类型
    cardStatuses, //水卡状态
    isFilter: false, //搜索高亮
    drawer: false, //搜索抽屉
    menuIndex: "", //展开更多菜单的列表项
    modelshow: true,
    modelstate: '', // 1解绑 2挂失 3解挂
    list: [],
    cardIndex: '', //操作数据的序号
    total: 0, //总条数
    nomore: false, //列表结束nomore
    loadmore: false, //列表更多loadmore
    empty: false, //空数据
    isModalSysn: false,
    modalSysnContent: `确认同步水卡余额？`
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.page = 1;
    this.getCardList();
  },

  reloadData() {
    this.page = 1;
    this.setData({
      loadmore: false,
      nomore: false,
      list: [],
      total: 0,
    })
    wx.showLoading()
    this.getCardList();
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },
  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  handleMore(e) {
    this.setData({
      menuIndex: e.currentTarget.dataset.index
    })
  },

  closeMenu() {
    this.setData({
      menuIndex: ""
    })
  },


  handleInput(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },
  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },


  onPullDownRefresh() {
    this.reloadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore) {
      ++this.page;
      this.getCardList();
    }
  },


  // 解绑
  Untie: function (e) {
    let card = e.currentTarget.dataset.card;
    let cardIndex = e.currentTarget.dataset.cardindex;
    this.setData({
      modelshow: false,
      modelstate: 1,
      cardIndex: cardIndex
    })
  },
  // 挂失
  reloss: function (e) {
    //console.log(e);
    let card = e.currentTarget.dataset.card;
    let cardIndex = e.currentTarget.dataset.cardindex;
    this.setData({
      modelshow: false,
      modelstate: 2,
      cardIndex: cardIndex
    })
  },
  // 解挂
  hangout: function (e) {
    let card = e.currentTarget.dataset.card;
    let cardIndex = e.currentTarget.dataset.cardindex;
    this.setData({
      modelshow: false,
      modelstate: 3,
      cardIndex: cardIndex
    })
  },
  //补卡
  supplementCard(e) {
    let card = e.currentTarget.dataset.card;
    let cardIndex = e.currentTarget.dataset.cardindex;
    this.setData({
      cardIndex: cardIndex
    })
    wx.navigateTo({
      url: '../supplementCard/supplementCard?card=' + e.currentTarget.dataset.card.card,
    })
  },
  // 关闭弹窗
  closemodel: function () {
    this.setData({
      modelshow: true
    })
  },



  //获取电子卡列表
  getCardList() {
    const conditions = this.data.conditions;
    app.ajax({
      url: 'Partner/WaterCard/waterCardList',
      data: {
        page: this.page,
        row: pageSize,
        salesman_id: wx.getStorageSync('salesman_id'),
        user_keyword: conditions.user_keyword,
        start_money: conditions.start_money,
        end_money: conditions.end_money,
        card_status: conditions.cardStatusIndex !== "" ? this.data.cardStatuses[conditions.cardStatusIndex].value : '',
        is_special: conditions.cardTypeIndex !== "" ? this.data.cardTypes[conditions.cardTypeIndex].value : "",
      },
      success: res => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code == 1000) {
          const source = res.data.data;
          let list = this.page == 1 ? source.data : this.data.list.concat(source.data);
          let total = source.page.total_number;
          this.setData({
            empty: total == 0 ? true : false,
            total,
            list,
            nomore: list.length >= total ? true : false,
            loadmore: list.length < total ? true : false,
            drawer: false
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: err => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误")
      }
    })
  },

  //确认操作
  okmodel(e) {
    var that = this;
    let type = e.currentTarget.dataset.type;
    that.closemodel();
    //确认解绑
    if (type == 1) {
      app.ajax({
        url: 'Salesman/Card/unbind',
        data: {
          sn: that.data.list[that.data.cardIndex].real_card
        },
        success: function (res) {
          //console.log(res)
          if (res.data.code == 1000) {
            that.reloadData();
            app.showToast('已成功解除绑定');
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    } else if (type == 2) { //确认挂失
      app.ajax({
        url: 'Salesman/Card/reportLoss',
        data: {
          id: that.data.list[that.data.cardIndex].water_id
        },
        success: function (res) {
          //console.log(res)
          if (res.data.code == 1000) {
            that.reloadData();
            app.showToast('已成功挂失');
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    } else if (type == 3) { //确认解挂
      app.ajax({
        url: 'Salesman/Card/unReportLoss',
        data: {
          id: that.data.list[that.data.cardIndex].water_id
        },
        success: function (res) {
          //console.log(res)
          if (res.data.code == 1000) {
            that.reloadData();
            app.showToast('已成功解挂');
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    }
  },
  hideSysn() {
    this.setData({
      isModalSysn: false
    })
    delete(this.water_id);
    delete(this.selectIndex)
  },

  handleSysn(e) {
    const index = e.detail.index;
    if (index == 0) {
      this.hideSysn()
    } else {
      if (this.submit) {
        app.showToast("正在处理中...");
        return false
      }
      this.submit = true;
      app.ajax({
        url: "Common/Card/syncCardBalance",
        data: {
          water_id: this.water_id,
        },
        success: res => {
          delete(this.submit);
          app.showToast(res.data.message);
          this.hideSysn();
        },
        error: e => {
          delete(this.submit);
          app.showToast("请求失败")
        }
      })
    }
  },

  showModalSysn(e) {
    const data = e.currentTarget.dataset;
    this.water_id = data.id;
    this.selectIndex = data.index;
    this.setData({
      isModalSysn: true,
      modalSysnContent: `确认同步水卡余额？`
    })
  },

})